package com.conorroche.taskmanagement;

import com.google.android.gms.common.GooglePlayServicesUtil;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends Activity implements ActionBar.TabListener{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);
		// Set up the action bar to show tabs.
		actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		// For each of the sections in the app, add a tab to the action bar.
		actionBar.addTab(actionBar.newTab().setText(R.string.title_activity_tasks)
				.setTabListener(this));
		actionBar.addTab(actionBar.newTab().setText(R.string.title_activity_tags)
				.setTabListener(this));
		actionBar.addTab(actionBar.newTab().setText(R.string.title_activity_location)
				.setTabListener(this));
		actionBar.setTitle("All Tasks");
	}
	protected void onResume(){
		super.onResume();
		actionBar.setSelectedNavigationItem(0);
		lst = (ListView) findViewById(R.id.TasksListView);
		tasksHelper = new TasksHelper(getApplicationContext(), lst);
		tasksHelper.getTaskView();
		ListView taskList = tasksHelper.getList();
		taskList.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				showTask = new Intent(getApplicationContext(), ViewTaskActivity.class);
				showTask.putExtra("id", tasksHelper.getTaskIds()[arg2]);
				startActivity(showTask);				
			}
		});
	}

	@Override
	public void onRestoreInstanceState(Bundle savedInstanceState) {
		// Restore the previously serialised current tab position.
		if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
			getActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
		}
	}
	@Override
	public void onSaveInstanceState(Bundle outState) {
		// Serialize the current tab position.
		outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar()
				.getSelectedNavigationIndex());
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        	case R.id.add_task:
        		addTask = new Intent(getApplicationContext(), AddTask.class);
            	addTask.putExtra("edit", false);
        		startActivity(addTask);
            	return true;
        	case R.id.add_tag:
        		addTag = new Intent(getApplicationContext(), AddTag.class);
        		startActivity(addTag);
        		return true;
        	case R.id.menu_legalnotices:
        		String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(getApplicationContext());
        	    AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(MainActivity.this);
        	    LicenseDialog.setTitle("Legal Notices");
        	    LicenseDialog.setMessage(LicenseInfo);
        	    LicenseDialog.show();
        	    return true;
        }
        return super.onOptionsItemSelected(item);
        
	}
	@Override
	public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
		switch(tab.getPosition()){
			case 0:	// all tasks
				
			break;
			case 1: //all tags
				Intent tagsIntent = new Intent(getApplicationContext(), TagsActivity.class);
				startActivity(tagsIntent);
			break;
			case 2:
				//Locations
				Intent map = new Intent(getApplicationContext(), LocationActivity.class);
				startActivity(map);
			break;
		}
	}
	@Override
	public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
		
	}
	@Override
	public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
	
	}
	/**
	 * The serialization (saved instance state) Bundle key representing the
	 * current tab position.
	 */
	private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
	private Intent addTask, addTag;
	private ListView lst;
	public static TasksHelper tasksHelper;
	public static TagsHelper tagsHelper;
	private Intent showTask;
	private ActionBar actionBar;

}

